require('./json2.js')


refreshObj = function(obj,data)
{
    for(i in data)
    {
        obj[i] = data[i]
    }
}

//将非stringl类型递归转化为string
xToString = function(obj)
{
    if (obj==null)
        return null 

    if(typeof(obj) == "string")
        return obj
    else if (typeof(obj) == "boolean")
        if(obj === true)
            return 'true'
        else
            return 'false'
    else if (typeof(obj) == "number")
        return obj
    else if (typeof(obj) == "object")
    {
        if (obj instanceof Array == true)
        {
            var str = ''
            for (var i=0;i<obj.length;i++)
            {    
                if (i==0) 
                {
                    str = str + xToString(obj[i])
                }
                else
                {
                    str = str + ',' + xToString(obj[i])
                }
            }   
            //str = str + ']'
            return str
        }
        else
        {
            return JSON.stringify(obj)  //json对象转换成json对符串  这个函数只适用于json对象，转化别的类型会出错 
        }
    }
}



clone = function(obj){  
    var o;  
    switch(typeof obj){  
    case 'undefined': break;  
    case 'string'   : o = obj + '';break;  
    case 'number'   : o = obj - 0;break;  
    case 'boolean'  : o = obj;break;  
    case 'object'   :  
        if(obj === null){  
            o = null;  
        }else{  
            if(obj instanceof Array){  
                o = [];  
                for(var i = 0, len = obj.length; i < len; i++){  
                    o.push(clone(obj[i]));  
                }  
            }else{  
                o = {};  
                for(var k in obj){  
                    o[k] = clone(obj[k]);  
                }  
            }  
        }  
        break;  
    default:          
        o = obj;break;  
    }  
    return o;     
}  




getObjLength = function(obj)
{   
    var length = 0
    for(var i in obj)
    {   
        length = length + 1
    }

    return length

}



function clone2(obj){  
    var o, obj;  
    if (obj.constructor == Object){  
        o = new obj.constructor();   
    }else{  
        o = new obj.constructor(obj.valueOf());   
    }  
    for(var key in obj){  
        if ( o[key] != obj[key] ){   
            if ( typeof(obj[key]) == 'object' ){   
                o[key] = clone2(obj[key]);  
            }else{  
                o[key] = obj[key];  
            }  
        }  
    }  
    o.toString = obj.toString;  
    o.valueOf = obj.valueOf;  
    return o;  
}  


function clone3(obj){  
    function Clone(){}  
    Clone.prototype = obj;  
    var o = new Clone();  
    for(var a in o){  
        if(typeof o[a] == "object") {  
            o[a] = clone3(o[a]);  
        }  
    }  
    return o;  
}  



